import {Component} from '@angular/core';
import {ViewController} from "ionic-angular";

@Component({
  selector: 'g-pay-keyboard',
  templateUrl: 'g-pay-keyboard.html'
})
export class GPayKeyboardComponent {

  value: string = ''

  constructor(public viewCtrl: ViewController) {
    //注册关闭回调
    this.viewCtrl.onWillDismiss(()=>{
      this.viewCtrl.data.onClose()
    })
    this.value = this.viewCtrl.data.defaultValue
  }

  change(code) {
    if (code == 'delete') {
      this.value = this.value.substr(0, this.value.length - 1)
    }else if(code == 'deleteAll'){
      this.value = ''
    }else{
      if (this.value.length < 6) {
        this.value += code
      }
    }
    this.viewCtrl.data.success(this.value)
    if(this.viewCtrl.data.autoClose && this.value.length == 6){
      this.close()
    }
  }

  close() {
    this.viewCtrl.dismiss();
  }

  isNumber(tar){
    return !isNaN(Number(tar))
  }

}
